var loadIndex = 0;
cc.Class({
    extends: cc.Component,

    properties: {
        maskSpriteFrame: cc.SpriteFrame,
        prefabs: [cc.Prefab],
        _prefabMap: [],

        _queue: [],
    },

    // use this for initialization
    onLoad: function () {
        // 单例设置为常驻节点
        cc.game.addPersistRootNode(this.node);
        // 赋值给全局方法
        GameToast.showPop = (prefab, closeCbk) => this.openPopup(prefab, closeCbk);
        GameToast.showPopRes = (prefabResPath, openCbk, closeCbk) => this.openPopupRes(prefabResPath,  openCbk, closeCbk);
        // 初始化prefabMap
        for (var i = 0; i < this.prefabs.length; i++) {
            var prefab = this.prefabs[i];
            this._prefabMap[prefab.name] = prefab;
        }
        // 错误提示
        this._errLog();
    },
    _errLog: function() {
        if (loadIndex === 0) {
            cc.log('popupManage初始化');
        }
        else if (loadIndex !== 0) {
            cc.error('错误：popupManage不能被挂到2个节点上', this.node.name);
        }
        loadIndex++;
    },
    openPopupRes: function(prefabResPath, openCbk, closeCbk) {
        cc.loadRes(prefabResPath, prefab => {
            var popNode = GameToast.showPop(prefab, closeCbk);
            openCbk && openCbk(popNode);
        }, true);
    },
    openPopup: function(prefab, closeCbk) {
        if (typeof(prefab) === 'string') {
            // 错误提示
            if (prefab === 'RechargePop' ||
                prefab === 'RankingListPop') { 
                cc.error('排行榜和充值必须动态加载方式弹出');
                return null;
            }

            if (!this._prefabMap[prefab]) {
                cc.error('弹窗管理中没有拖入弹窗:' + prefab);
                return null;
            }
            prefab = this._prefabMap[prefab];
        }

        var node = cc.instantiate(prefab);
        var popupProxy = node.getComponent("PopupProxy");
        if (!popupProxy) {
            cc.error('弹窗界面没有添加弹窗代理PopupProxy');
            return null;
        };
        closeCbk && popupProxy.node.on('close', closeCbk);
        this.addPopup(node, popupProxy.isTouchMask);
        return node;
    },

    addPopup: function(prefabNode, isTouchMask) {
        cc.logEx('打开pop弹窗');

        // 访问网络 && 播放动画 => 都完成后 => 就绪
        var popupNode = isTouchMask ? this._newTouchMaskPopupNode() : this._newPopupNode();
        this.node.addChild(popupNode);

        prefabNode.on('onClose', this._onClosePopup, this);
        prefabNode.on('close', this._closePopup, this);
        prefabNode._popupNode = popupNode;
        popupNode.addChild(prefabNode);
        
        var popElement = {};
        popElement._popupNode = popupNode;
        this._queue.push(popElement);
    },

    _newPopupNode: function() {
        var newNode = new cc.Node();
        var widget = newNode.addComponent(cc.Widget);
        widget.isAlignTop = true;
        widget.isAlignBottom = true;
        widget.isAlignLeft = true;
        widget.isAlignRight = true;
        widget.top = 0;
        widget.bottom = 0;
        widget.left = 0;
        widget.right = 0;
        widget.isAlignOnce = true;
        return newNode;
    },
    _newTouchMaskPopupNode: function() {
        var newNode = this._newPopupNode();
        newNode.color = cc.color(0, 0, 0);
        newNode.opacity = 153;
        newNode.cascadeOpacity = false;

        var sprite = newNode.addComponent(cc.Sprite);
        sprite.spriteFrame = this.maskSpriteFrame;
        sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
        sprite.type = cc.Sprite.Type.SLICED;

        newNode.addComponent('TouchMask');
        return newNode;
    },

    _onClosePopup: function(event) {
        cc.logEx('关闭pop弹窗');
        var newNode = this._newPopupNode();
        newNode.addComponent('TouchMask');
        event.target._popupNode.addChild(newNode);
    },
    _closePopup: function(event) {
        cc.logEx('销毁pop弹窗');
        for (var i = 0; i < this._queue.length; i++) {
            var element = this._queue[i];
            if (element._popupNode === event.target._popupNode) {
                element._popupNode.destroy();
                this._queue.splice(i, 1);
                break;
            }
        }
    },
});